package sunstar.com.xq.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import sunstar.com.xq.R;

/**
 * Created by Sun.Star on 2017/6/9.
 */

public class ScrollViewTextView extends View {
    private String text = "";
    private int textSize;
    private int textColor;
    private int backColor;
    private Rect rect;
    private Paint paint;
    private int curPosition = 0;
    private float movePrecent = 0f;
    public void changeText(int curPosition,float movePrecent){
        this.curPosition = curPosition;
        this.movePrecent = movePrecent;
        invalidate();
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public void setBackColor(int backColor) {
        this.backColor = backColor;
    }

    public ScrollViewTextView(Context context) {
        this(context,null);
    }

    public ScrollViewTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScrollViewTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray type = context.
                obtainStyledAttributes(attrs, R.styleable.ScrollViewTextView,
                        defStyleAttr,0);
        text = type.getString(R.styleable.ScrollViewTextView_text);
        textColor = type.getColor(R.styleable.ScrollViewTextView_text_color, Color.BLACK);
        backColor = type.getColor(R.styleable.ScrollViewTextView_back_color, Color.WHITE);
        textSize = type.getDimensionPixelSize(R.styleable.ScrollViewTextView_text_size,(int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 14, getResources().getDisplayMetrics()));
        type.recycle();
        typeface = Typeface.createFromAsset(context.getAssets(), "fonts/qingkai.ttf");
        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setTypeface(typeface);
        paint.setTextSize(textSize);
        rect = new Rect();

    }
    private Typeface typeface;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if(widthMode == MeasureSpec.EXACTLY){
            width = widthSize;
        }else{
            paint.setTextSize(textSize);
            paint.getTextBounds(text,0,text.length(),rect);
            float textSize = rect.width();
            width = (int)(textSize+getPaddingLeft()+getPaddingRight());
        }
        if(heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else{
            paint.setTextSize(textSize);

            paint.getTextBounds(text,0,text.length(),rect);
            float textHeight = rect.height();
            height = (int) (textHeight+getPaddingTop()+getPaddingBottom());
        }
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(backColor);
        canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),paint);
        paint.setColor(textColor);
        String[] finalString = text.split("，");
        if(finalString.length == 1){
            paint.getTextBounds(finalString[curPosition],0,finalString[curPosition].length(),rect);
            canvas.drawText(finalString[0],(getWidth()/2)-(rect.width()/2),(getHeight()/2)+(rect.height()/2),paint);
        }else{
            if(movePrecent==0){
                Log.d("ScrollViewTextView", "执行");
                paint.getTextBounds(finalString[curPosition],0,finalString[curPosition].length(),rect);
                canvas.drawText(finalString[curPosition],(getWidth()/2)-(rect.width()/2),(getHeight()/2)+(rect.height()/2),paint);
            }else{
                paint.setAlpha((int) (1-movePrecent*255));
                paint.getTextBounds(finalString[curPosition],0,finalString[curPosition].length(),rect);
                canvas.drawText(finalString[curPosition],(getWidth()/2)-(rect.width()/2),(getHeight()/2)+(rect.height()/2)-(((getHeight()/2)+(rect.height()/2))*movePrecent),paint);
                paint.setAlpha((int) ((movePrecent)*255));
                paint.getTextBounds(finalString[curPosition+1],0,finalString[curPosition+1].length(),rect);
                canvas.drawText(finalString[curPosition+1],(getWidth()/2)-(rect.width()/2),(getHeight()+rect.height()/2)-((getHeight()/2)*movePrecent),paint);
            }
        }
    }


}
